package com.wudi.estate.controller.ownermanagement;

import com.github.pagehelper.PageInfo;
import com.wudi.estate.bean.ownermanagement.MemberType;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.bean.ownermanagement.Pet;
import com.wudi.estate.service.ownermanagement.PersonnelService;
import com.wudi.estate.service.ownermanagement.PetService;
import com.wudi.estate.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Controller
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private PetService petService;
    @Autowired
    private PersonnelService personnelService;
    @RequestMapping("/listUi")
    public String pet_listUi(String somename,ModelMap map,Integer pageNum){
        if(pageNum==null){
            pageNum =1;
        }
        try {
            // 假设第一个启动页面， 当前页数1，每页显示的数量3
            // 通过业务层获得分页信息
            PageInfo<Map<String, Object>> pageInfo = petService.selectAllTwo(pageNum, 3);
            map.addAttribute("pageInfo",pageInfo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
//        if (somename==null&&somename==""){
//            try {
//                List<Pet> ps =petService.selectAll();
//                List<Map<String, Object>> pets = insertList(ps);
//                map.addAttribute("pets",pets);
//
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        }else{
//            try {
//                List<Pet> ps=petService.selectByName(somename);
//                List<Map<String, Object>> pets = insertList(ps);
//                map.addAttribute("pets",pets);
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
//        }
        return "pet_list";
    }
    public List<Map<String,Object>> insertList(List<Pet> pet) throws Exception {
        List<Map<String,Object>> pets = new ArrayList<>();
        for (Pet p: pet) {
            Map<String,Object> m =new HashMap<>();
            Personnel per = new Personnel();
            per.setId(p.getPid());
            Personnel personnel = personnelService.selectById(per);
            m.put("id",p.getId());
            m.put("img",p.getImg());
            m.put("pname",personnel.getName());
            m.put("name",p.getName());
            m.put("color",p.getColor());
            m.put("remark",p.getRemark());
            m.put("adoptTime",p.getAdoptTime());
            m.put("createTime",p.getCreateTime());
            m.put("state",p.isState());
            pets.add(m);
        }
        return pets;
    }
    @RequestMapping("/addUi")
    public String pet_addUi(ModelMap map){
        List<Personnel> ps = new ArrayList<>();
        try {
           ps = personnelService.selectAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("ps",ps);
        return "pet_add";
    }
    @RequestMapping("/add")
    public String pet_add(Pet pet, MultipartFile headerFile){

        String fileName = "";

        try {
            fileName = FileUtils.fileUpload(headerFile);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        pet.setImg(fileName);
        pet.setCreateTime(new Date());
        System.out.println(pet);
        try {
            petService.insert(pet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/pet/listUi";
    }
    @RequestMapping("/editUi")
    public String pet_editUi(long id,ModelMap map,ModelMap map1){
        List<Personnel> ps = new ArrayList<>();
        try {
            ps = personnelService.selectAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map1.addAttribute("ps",ps);
        Pet pet = new Pet();
        pet.setId(id);
        Pet pet1 = null;
        try {
            pet1 = petService.selectById(pet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("pet1",pet1);
        return "pet_edit";
    }
    @RequestMapping("/edit")
    public String pet_edit(Pet pet ,MultipartFile headerFile){
        String fileName = "";
        System.out.println(headerFile);
        if(!headerFile.isEmpty()){
            try {
                fileName = FileUtils.fileUpload(headerFile);


            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            pet.setImg(fileName);
        }
        try {
            petService.updateById(pet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/pet/listUi";
    }
    @RequestMapping("/update")
    public String pet_update_state(long id){
        Pet p = new Pet();
        p.setId(id);
        try {
            petService.updateStateById(p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/pet/listUi";
    }

}

